package com.neu.edu.boonpay.config;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class MyInterceptor implements HandlerInterceptor {
    private Set<String> requestIds = ConcurrentHashMap.newKeySet();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        // String url =request.getRequestURI();// 获得请求url
        String currentIp = request.getRemoteAddr();
        // 防止重复提交
        if (requestIds.contains(currentIp)) {
            return false;
        }
        requestIds.add(currentIp);
        return true;

    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        String currentIp = request.getRemoteAddr();
        requestIds.remove(currentIp);

    }


}
